<?php
/**
 * 办公管理控制器
 * @author juntao
 *
 */
class OfficeController extends App_Zend_Controller_Action_Admin
{
	/**该方法主要进行的是权限判断*/
	public function init() 
	{
		$user = App_User::getCurrentUser();
		if($user){
			if(!in_array('5', json_decode($user->permission))){
				//$this->_request->setControllerName('error');
				exit('You have no priviage');
			}
		}else{
			$this->_redirect('/user/login');
		}
		parent::init();		
	}

	/**异步加载每组用户的信息*/
	public function loaduserAction()
	{
		//禁用视图渲染与布局以便能够ajax向原页面echo输出需要返回的值，否则会提示找不到log/imloadstock.phtml文件
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();

		$userModel =  App_Model::factory('User');
		
		$type = $this->getRequest()->getParam('group', '');//用户类型
		$users = $userModel->getByType($type)->toArray();
		$content = '';
		foreach ($users as $v){
			$content = $content."<li><a data-id='".$v['user_id']."'>".$v['true_name']."</a></li>";
		}
		
		echo json_encode(array('content'=>$content));
	}
	
	/**发布需求*/
	public function pubneedAction()
	{
		if($this->getRequest()->isPost()) {
		    $qaitem = App_Model::factory('QaItem');
		    
			$title = $this->getRequest()->getParam('title', '');
			$site = $this->getRequest()->getParam('site', '');
			$content = htmlspecialchars_decode($this->getRequest()->getParam('editor', ''));
			$asignUser = $this->getRequest()->getParam('asign_user', '');
			$fromUser = $this->getRequest()->getParam('from_user', '');
			$priority = $this->getRequest()->getParam('priority','0');
			$memo = $this->getRequest()->getParam('memo','');
			$permission = $this->getRequest()->getParam('permission');
			
			$msg = array();
			if (empty($title)) {
				$msg[] = '标题不能为空';
			}
	
			if(empty($site)){
				$msg[] = '网站选择不能为空';
			}
				
			if(empty($content)) {
				$msg[] = '详情不能为空';
			} 

			$flag = false;
			foreach($asignUser as $v) {
				if(!empty($v)){
					$flag = true;
				}
			}
			if(!$flag){
				$msg[] = '用户指派不能为空';
			}
			
			if(empty($fromUser)) {
				$fromUser = $_SESSION['User']['user']['user_id'];
			}
			
			if (empty($msg)) {
				foreach ($asignUser as $au){
					$row = array('need_id'=>$fromUser, 'task_id'=>$au, 'title'=>$title, 'site'=>$site, 'content'=>$content, 'priority'=>$priority, 'statu'=>'0', 'memo'=>$memo, 'add_time'=>date('Y-m-d H:i:s', time()), 'asign_time'=>date('Y-m-d H:i:s', time()));
					if ($qaitem->add($row)) {
						$mailer = App_Model::factory('Mail');
						$userModel = App_Model::factory('User');
						$user = $userModel->getOne($au);
						$to = $user->email;
						$subject = $title.'('.$site.')';
						$message = stripslashes($content);
						$from = "admin@taotens.com";
						$mailer->sendEmail($from, $to, $subject, $message);//发送邮件给用户
							
						$msg[] = '需求发布成功';
					} else {
						$msg[] = '需求发布失败';
					}
					unset($row);
				}
				$this->view->msg = $msg;
			}else{
				$this->view->msg = $msg;
			}
		}
		$this->view->user = $_SESSION['User']['user']['user_name'];
	}
	
	/**我的需求*/
	public function myneedAction()
	{
		$qaitem = App_Model::factory('QaItem');
		$userModel = App_Model::factory('User');
		$qatimeproModel = App_Model::factory('QaTimepro');

		$page = $this->getRequest()->getParam('page',1);
		$title = $this->getRequest()->getParam('title');
		$site = $this->getRequest()->getParam('site');
		$statu = $this->getRequest()->getParam('statu','');
		$starttime = $this->getRequest()->getParam('starttime');
		$endtime = $this->getRequest()->getParam('endtime');
		$needid = $_SESSION['User']['user']['user_id'];
		$taskid = $this->getRequest()->getParam('task_id','');
		
		$limit = 30;//每页显示数
		$list  = $qaitem->listAll($needid,$taskid,$title,$site,$statu,$starttime,$endtime,$page,$limit)->toArray();
		/**增加工时进度的内容*/
		foreach ($list as &$v){
			$v['task_name'] = $userModel->getOne($v['task_id'])->true_name;
			$v['need_name'] = $userModel->getOne($v['need_id'])->true_name;
			$timeprolist = $qatimeproModel->getQaTimepro(array('qa_item'=>$v['id']))->toArray();
			$time = 0;
			$progress = array();
			$progress[] = 0;
			foreach ($timeprolist as $tpv){
				$time += $tpv['time'];
				$progress[] = $tpv['progress'];
			}
			rsort($progress);
			$v['time'] = $time;
			$v['progress'] = $progress[0];
		}
		
		/**生成分页对象*/
		$num = $qaitem->getTotalNum($needid,$taskid,$title,$site,$statu,$starttime,$endtime);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($num));
		$paginator->setItemCountPerPage($limit); // 每页30条
		$paginator->setCurrentPageNumber($page);
		$this->view->paginator = $paginator ; //将paginator传到view层
		
		$this->view->list = $list;
		$this->view->title = $title;
		$this->view->site = $site;
		$this->view->statu = $statu;
		$this->view->starttime = $starttime;
		$this->view->endtime = $endtime;
	}

	/**异步查看任务需求信息*/
	public function viewqaitemAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		$qaitemModel = App_Model::factory('QaItem');
		$qatimeproModel = App_Model::factory('QaTimepro');
		$userModel = App_Model::factory('User');
		$id = $this->getRequest()->getParam('id', '');
		$qaitem = $qaitemModel->getOne($id)->toArray();
		$user0 = $userModel->getOne($qaitem['need_id']);
		$user1 = $userModel->getOne($qaitem['task_id']);
		
		//已用工时和进度
		$qatimepro = $qatimeproModel->getQaTimepro(array('qa_item'=>$id))->toArray();
		$time = 0;
		$pro = 0;
		foreach ($qatimepro as $v){
			$time = $time+intval($v['time']);
			$pro = intval($v['progress']);
		}
		$timepro = '<div class="form-group form-inline"><label class="col-sm-2">工时进度：</label><div class="col-sm-10">已用工时 '.$time.'小时；目前进度 '.$pro.'%</div></div>';
		
		$html = '<div class="modal-header"><button class="close" type="button" data-dismiss="modal">×</button><h4 class="modal-title">'.$qaitem['site'].
		        '</h4></div><div class="modal-body"><div class="form-group form-inline"><label class="col-sm-2">摘要：</label><div class="col-sm-10">'.
		        $qaitem['title'].'</div></div><div class="form-group form-inline"><label class="col-sm-2">详情：</label><div class="col-sm-10">'.stripslashes($qaitem['content']).
		        '</div></div><div class="form-group form-inline"><label class="col-sm-2">来自于：</label><div class="col-sm-10">'.$user0['user_name'].
		        '</div></div><div class="form-group form-inline"><label class="col-sm-2">指派给：</label><div class="col-sm-10">'.$user1['user_name'].
		        '</div></div><div class="form-group form-inline"><label class="col-sm-2">优先级：</label>'.
                '<div class="col-sm-10">'.($qaitem['priority']==2?'十分紧急':($qaitem['priority']==1?'比较重要':'默认等待')).
                '</div></div><div class="form-group form-inline"><label class="col-sm-2">状态：</label><div class="col-sm-10">'.
                ($qaitem['statu']==2?'已完成':($qaitem['statu']==1?'进行中':'未开始')).
                '</div></div><div class="form-group form-inline"><label class="col-sm-2">发布时间：</label><div class="col-sm-10">'.$qaitem['add_time'].
                '</div></div><div class="form-group form-inline"><label class="col-sm-2">开始时间：</label><div class="col-sm-10">'.$qaitem['start_time'].
                '</div></div><div class="form-group form-inline"><label class="col-sm-2">完成时间：</label><div class="col-sm-10">'.$qaitem['end_time'].
                '</div></div>'.$timepro.'<div class="form-group form-inline"><label class="col-sm-2">备注：</label><div class="col-sm-10">'.(empty($qaitem['memo'])?'无':$qaitem['memo']).
                '</div></div><div class="form-group form-inline"><label class="col-sm-2">评价：</label><div class="col-sm-10">'.(empty($qaitem['evaluate'])?'无':$qaitem['evaluate']).
                '</div></div></div>';
		
		echo json_encode(array('html'=>$html));
	}
	
	/**更新需求*/
	public function updateneedAction()
	{
		$qaitemModel = App_Model::factory('QaItem');
		$userModel = App_Model::factory('User');
		if($this->getRequest()->isPost()) {
			$id = $this->getRequest()->getParam('id', '');
			$title = $this->getRequest()->getParam('title', '');
			$site = $this->getRequest()->getParam('site', '');
			$content = htmlspecialchars_decode($this->getRequest()->getParam('editor', ''));
			$asignUser = $this->getRequest()->getParam('asign_user', '');
			$fromUser = $this->getRequest()->getParam('from_user', '');
			$priority = intval($this->getRequest()->getParam('priority','0'));
			$memo = $this->getRequest()->getParam('memo','');
			$evaluate = $this->getRequest()->getParam('evaluate','');
				
			$msg = array();
			if (empty($title)) {
				$msg[] = '标题不能为空';
			}
				
			if(empty($site)){
				$msg[] = '网站选择不能为空';
			}
				
			if(empty($content)) {
				$msg[] = '详情不能为空';
			}
				
			if(empty($asignUser)) {
				$msg[] = '用户指派不能为空';
			}
				
			if(empty($fromUser)) {
				$msg[] = '需求来自不能为空';
			}
				
			if (empty($msg)) {
				$row = array('need_id'=>$fromUser, 'task_id'=>$asignUser, 'title'=>$title, 'site'=>$site, 'content'=>$content, 'priority'=>$priority, 'memo'=>$memo, 'evaluate'=>$evaluate);
	
				if ($qaitemModel->update($row,$id)) {
					$mailer = App_Model::factory('Mail');
					$userModel = App_Model::factory('User');
					$user = $userModel->getOne($asignUser);
					$to = $user->email;
					$subject = $title.'('.$site.'-update demand)';
					$message = $content;
					$from = "admin@taotens.com";
					$mailer->sendEmail($from, $to, $subject, $message);//发送邮件给用户
						
					$msg[] = '需求更新成功';
				} else {
					$msg[] = '需求更新失败';
				}
				$this->view->msg = $msg;
			}else{
				$this->view->msg = $msg;
			}
				
			$qaitem = $qaitemModel->getOne($id)->toArray();
			$asign = $userModel->getOne($qaitem['task_id']);
			$qaitem['task_name'] = $asign->true_name;
			$qaitem['asign_group'] = $asign->user_type;
			$qaitem['need_name'] = $userModel->getOne($qaitem['need_id'])->true_name;
			$this->view->qaitem = $qaitem;
		}else{
			$id = $this->getRequest()->getParam('id', '');
			if (!empty($id)){
				$qaitem = $qaitemModel->getOne($id)->toArray();
				$asign = $userModel->getOne($qaitem['task_id']);
				$qaitem['task_name'] = $asign->true_name;
				$qaitem['asign_group'] = $asign->user_type;
				$qaitem['need_name'] = $userModel->getOne($qaitem['need_id'])->true_name;
				$this->view->qaitem = $qaitem;
			}
		}
	}

	/**评价任务完成情况*/
	public function evaluateAction()
	{
		$qaitemModel = App_Model::factory('QaItem');
		$userModel = App_Model::factory('User');
		$id = $this->getRequest()->getParam('id', '');
		if($this->getRequest()->isPost()) {
		    $evaluate = $this->getRequest()->getParam('evaluate');
		    $row = array('evaluate'=>$evaluate);
		    if($qaitemModel->update($row,$id)){
		    	$this->_redirect('/office/myneed') ;
		    }
		}
		$id = $this->getRequest()->getParam('id', '');
		if (!empty($id)){
			$qaitem = $qaitemModel->getOne($id)->toArray();
			$asign = $userModel->getOne($qaitem['task_id']);
			$qaitem['task_name'] = $asign->true_name;
			$qaitem['asign_group'] = $asign->user_type;
			$qaitem['need_name'] = $userModel->getOne($qaitem['need_id'])->true_name;
			$this->view->qaitem = $qaitem;
		    $this->view->id = $id;
		}else{
			$this->view->msg = 'error';
		}
	}
	
	/**备注任务需求情况*/
	public function memoAction()
	{
		$qaitemModel = App_Model::factory('QaItem');
		$userModel = App_Model::factory('User');
		$id = $this->getRequest()->getParam('id', '');
		
		if (!empty($id)){
			$qaitem = $qaitemModel->getOne($id)->toArray();
			if($this->getRequest()->isPost()) {
				$memo = $this->getRequest()->getParam('memo');
				$row = array('memo'=>$memo);
				if($qaitemModel->update($row,$id)){
					$mailer = App_Model::factory('Mail');
					$userNeed = $userModel->getOne($qaitem['need_id']);
					$toNeed = $userNeed->email;
					$userTask = $userModel->getOne($qaitem['task_id']);
					$toTask = $userTask->email;
					$subject = $qaitem['title'].'('.$qaitem['site'].'-memo attention)';
					$message = $qaitem['content'] . '<br>' . '备注：' .$memo;
					$from = "admin@taotens.com";
					$mailer->sendEmail($from, $toNeed, $subject, $message);//发送邮件给用户
					$mailer->sendEmail($from, $toTask, $subject, $message);//发送邮件给用户
					$this->_redirect('/office/memo/id/'.$id) ;
				}
			}
			
			$qaitem = $qaitemModel->getOne($id)->toArray();
			$asign = $userModel->getOne($qaitem['task_id']);
			$qaitem['task_name'] = $asign->true_name;
			$qaitem['asign_group'] = $asign->user_type;
			$qaitem['need_name'] = $userModel->getOne($qaitem['need_id'])->true_name;
			$this->view->qaitem = $qaitem;
			$this->view->id = $id;
		}else{
			$this->view->msg = 'error';
		}
	}
	
	/**删除需求*/
	public function deleteqaitemAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
		
		$qaitemModel = App_Model::factory('QaItem');
		$id = $this->getRequest()->getParam('id', '');
		$id = intval($id);
		
		if ($qaitemModel->delete($id)) {
			echo 1;
		} else {
			echo 0;
		}
	}
	
	/**需求总览*/
	public function needsAction()
	{
		$qaitem = App_Model::factory('QaItem');
		$userModel = App_Model::factory('User');
		$qatimeproModel = App_Model::factory('QaTimepro');
	
		$page = $this->getRequest()->getParam('page',1);
		$title = $this->getRequest()->getParam('title');
		$site = $this->getRequest()->getParam('site');
		$statu = $this->getRequest()->getParam('statu','');
		$starttime = $this->getRequest()->getParam('starttime');
		$endtime = $this->getRequest()->getParam('endtime');
		$taskid = $this->getRequest()->getParam('task_id','');
		$needid = $this->getRequest()->getParam('asign_user','');
	
		$limit = 10;//每页显示数
		$list  = $qaitem->listAll($needid,$taskid,$title,$site,$statu,$starttime,$endtime,$page,$limit)->toArray();
		/**增加工时进度的内容*/
		foreach ($list as &$v){
			$v['task_name'] = $userModel->getOne($v['task_id'])->true_name;
			$v['need_name'] = $userModel->getOne($v['need_id'])->true_name;
			$timeprolist = $qatimeproModel->getQaTimepro(array('qa_item'=>$v['id']))->toArray();
			$time = 0;
			$progress = array();
			$progress[] = 0;
			foreach ($timeprolist as $tpv){
				$time += $tpv['time'];
				$progress[] = $tpv['progress'];
			}
			rsort($progress);
			$v['time'] = $time;
			$v['progress'] = $progress[0];
		}
		/**生成分页对象*/
		$num = $qaitem->getTotalNum($needid,$taskid,$title,$site,$statu,$starttime,$endtime);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($num));
		$paginator->setItemCountPerPage($limit); // 每页30条
		$paginator->setCurrentPageNumber($page);
		$this->view->paginator = $paginator ; //将paginator传到view层
		$this->view->taskid = $taskid;
		$this->view->list = $list;
		$this->view->task_name = trim($userModel->getOne($needid)->true_name);
	}
	
	/**我的任务*/
	public function mytaskAction()
	{
		$qaitem = App_Model::factory('QaItem');
		$userModel = App_Model::factory('User');
		$qatimeproModel = App_Model::factory('QaTimepro');

		$page = $this->getRequest()->getParam('page',1);
		$title = $this->getRequest()->getParam('title');
		$site = $this->getRequest()->getParam('site');
		$statu = $this->getRequest()->getParam('statu','');
		$starttime = $this->getRequest()->getParam('starttime');
		$endtime = $this->getRequest()->getParam('endtime');
		$needid = $this->getRequest()->getParam('need_id','');
		$taskid = $_SESSION['User']['user']['user_id'];
		//$needid = $_SESSION['User']['user']['user_id'];
		//$taskid = $this->getRequest()->getParam('task_id','');
		
		$limit = 30;//每页显示数
		$list  = $qaitem->listAll($needid,$taskid,$title,$site,$statu,$starttime,$endtime,$page,$limit)->toArray();
		/**增加工时进度的内容*/
		foreach ($list as &$v){
			$v['task_name'] = $userModel->getOne($v['task_id'])->true_name;
			$v['need_name'] = $userModel->getOne($v['need_id'])->true_name;
		    $timeprolist = $qatimeproModel->getQaTimepro(array('qa_item'=>$v['id']))->toArray();
		    $time = 0;
		    $progress = array();
		    $progress[] = 0;
		    foreach ($timeprolist as $tpv){
		    	$time += $tpv['time'];
		    	$progress[] = $tpv['progress'];
		    }
		    rsort($progress);
		    $v['time'] = $time;
		    $v['progress'] = $progress[0];
		}
		
		/**生成分页对象*/
		$num = $qaitem->getTotalNum($needid,$taskid,$title,$site,$statu,$starttime,$endtime);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($num));
		$paginator->setItemCountPerPage($limit); // 每页30条
		$paginator->setCurrentPageNumber($page);
		$this->view->paginator = $paginator ; //将paginator传到view层
		
		/**未开始的任务*/
		$tblist = $qaitem->getQaItem(array('statu'=>0,'task_id'=>$taskid))->toArray();
		foreach ($tblist as &$vv){
			$vv['task_name'] = $userModel->getOne($vv['task_id'])->true_name;
			$vv['need_name'] = $userModel->getOne($vv['need_id'])->true_name;
		}
		/**正在进行的任务*/
		$tflist = $qaitem->getQaItem(array('statu'=>1,'task_id'=>$taskid))->toArray();
		foreach ($tflist as &$vvv){
			$vvv['task_name'] = $userModel->getOne($vvv['task_id'])->true_name;
			$vvv['need_name'] = $userModel->getOne($vvv['need_id'])->true_name;
		}
		
		$this->view->tblist = $tblist;
		$this->view->tflist = $tflist;
		$this->view->list = $list;
		$this->view->title = $title;
		$this->view->site = $site;
		$this->view->statu = $statu;
		$this->view->starttime = $starttime;
		$this->view->endtime = $endtime;
	}

	/**开始任务*/
	public function beginAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
	
		$qa_id = $this->getRequest()->getParam('id', 0);
		$qa_id = intval($qa_id);
		if($qa_id!=0){
			$qaitemModel = App_Model::factory('QaItem');
			$qaitem = $qaitemModel->getOne($qa_id);
			if($qaitemModel->update(array('statu'=>1,'start_time'=>date('Y-m-d H:i:s', time())),$qa_id)){
				$mailer = App_Model::factory('Mail');
				$userModel = App_Model::factory('User');
				$user = $userModel->getOne($qaitem->need_id);
				$to = $user->email;
				$subject = $qaitem->title;
				$message = '你的任务需求：【'.$qaitem->title.'】开始了！';
				$from = "admin@taotens.com";
				$mailer->sendEmail($from, $to, $subject, $message);
				echo 1;
			}else{
				echo 0;
			}
		}
	}
	
	/**完成任务*/
	public function finishAction()
	{
		$this->getHelper('viewRenderer')->setNoRender();
		$this->getHelper('layout')->disableLayout();
	
		$qa_id = $this->getRequest()->getParam('id', 0);
		$qa_id = intval($qa_id);
		if($qa_id!=0){
			$qaitemModel = App_Model::factory('QaItem');
			$qaitem = $qaitemModel->getOne($qa_id);
			if($qaitemModel->update(array('statu'=>2,'end_time'=>date('Y-m-d H:i:s', time())),$qa_id)){
				$mailer = App_Model::factory('Mail');
				$userModel = App_Model::factory('User');
				$user = $userModel->getOne($qaitem->need_id);
				$to = $user->email;
				$subject = $qaitem->title;
				$message = '你的任务需求：【'.$qaitem->title.'】完成了！';
				$from = "admin@taotens.com";
				$mailer->sendEmail($from, $to, $subject, $message);
				echo 1;
			}else{
				echo 0;
			}
		}
	}

	/**任务总览*/
	public function tasksAction()
	{
		$qaitem = App_Model::factory('QaItem');
		$userModel = App_Model::factory('User');
		$qatimeproModel = App_Model::factory('QaTimepro');
	
		$page = $this->getRequest()->getParam('page',1);
		$title = $this->getRequest()->getParam('title');
		$site = $this->getRequest()->getParam('site');
		$statu = $this->getRequest()->getParam('statu','');
		$starttime = $this->getRequest()->getParam('starttime');
		$endtime = $this->getRequest()->getParam('endtime');
		$needid = $this->getRequest()->getParam('need_id','');
		$taskid = $this->getRequest()->getParam('asign_user','');
	
		$limit = 10;//每页显示数
		$list  = $qaitem->listAll($needid,$taskid,$title,$site,$statu,$starttime,$endtime,$page,$limit)->toArray();
		/**增加工时进度的内容*/
		foreach ($list as &$v){
			$v['task_name'] = $userModel->getOne($v['task_id'])->true_name;
			$v['need_name'] = $userModel->getOne($v['need_id'])->true_name;
			$timeprolist = $qatimeproModel->getQaTimepro(array('qa_item'=>$v['id']))->toArray();
			$time = 0;
			$progress = array();
			$progress[] = 0;
			foreach ($timeprolist as $tpv){
				$time += $tpv['time'];
				$progress[] = $tpv['progress'];
			}
			rsort($progress);
			$v['time'] = $time;
			$v['progress'] = $progress[0];
		}
		
		/**生成分页对象*/
		$num = $qaitem->getTotalNum($needid,$taskid,$title,$site,$statu,$starttime,$endtime);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($num));
		$paginator->setItemCountPerPage($limit); // 每页30条
		$paginator->setCurrentPageNumber($page);
		$this->view->paginator = $paginator ; //将paginator传到view层
	
		if(!empty($taskid)){
			/**未开始的任务*/
			$tblist = $qaitem->getQaItem(array('statu'=>0,'task_id'=>$taskid))->toArray();
			foreach ($tblist as &$vv){
				$vv['task_name'] = $userModel->getOne($vv['task_id'])->true_name;
				$vv['need_name'] = $userModel->getOne($vv['need_id'])->true_name;
			}
			/**正在进行的任务*/
			$tflist = $qaitem->getQaItem(array('statu'=>1,'task_id'=>$taskid))->toArray();
			foreach ($tflist as &$vvv){
				$vvv['task_name'] = $userModel->getOne($vvv['task_id'])->true_name;
				$vvv['need_name'] = $userModel->getOne($vvv['need_id'])->true_name;
			}
				
			$this->view->tblist = $tblist;
			$this->view->tflist = $tflist;
			$this->view->taskid = $taskid;
			$this->view->task_name = trim($userModel->getOne($taskid)->true_name);
		}
	
		$this->view->list = $list;
		$this->view->title = $title;
		$this->view->site = $site;
		$this->view->statu = $statu;
		$this->view->starttime = $starttime;
		$this->view->endtime = $endtime;
	}
	
	/**我的日报*/
	public function mydailyAction()
	{
		$userModel = App_Model::factory('User');
		$daily = App_Model::factory('Daily');

		$page = $this->getRequest()->getParam('page',1);
		$title = $this->getRequest()->getParam('title');
		$starttime = $this->getRequest()->getParam('starttime');
		$endtime = $this->getRequest()->getParam('endtime');
		$adduser = $userModel->exist($_SESSION['User']['user']['user_name'])->user_id;
		
		$limit = 20;//每页显示数
		$list = $daily->listAll($title,$adduser,$starttime,$endtime,$page,$limit)->toArray();
		/**生成分页对象*/
		$num = $daily->getTotalNum($title,$adduser,$starttime,$endtime);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($num));
		$paginator->setItemCountPerPage($limit); // 每页20条
		$paginator->setCurrentPageNumber($page);
		
		$this->view->list = $list;
		$this->view->user_name = $_SESSION['User']['user']['user_name'];
		$this->view->paginator = $paginator ; //将paginator传到view层
	}
	
	/**发布日报*/
	public function pubdailyAction()
	{
		if($this->getRequest()->isPost()) {
		    $daily = App_Model::factory('Daily');
		    $qatimepro = App_Model::factory('QaTimepro');
		    
			$content = htmlspecialchars_decode($this->getRequest()->getParam('editor', ''));
			$qa_ids = $this->getRequest()->getParam('qa_ids', '');
			$dotime = $this->getRequest()->getParam('dotime', '');
			$doprogress = $this->getRequest()->getParam('doprogress', '');
			
			$msg = array();
			if (empty($content)) {
				$msg[] = '日报内容不能为空';
			}
			
			if(!empty($dotime)){
				if (empty($qa_ids) ) {
					$msg[] = '关联任务不能为空';
				}else{
					$pattern = '/[0-9]+/';
					$pat = '/[0-9]*(\.[0-9]{0,1})?$/';
					foreach($qa_ids as $id){
						if(empty($dotime[$id]) || empty($doprogress[$id])){
							$msg[] = '发现关联的任务没有对应的工时或进度';
						}else{
							preg_match($pat, $dotime[$id], $matches);//匹配获取数字部分
							if($matches[0] != $dotime[$id]){
								$msg[] = '填写工时格式不正确，必须数字或1位小数';
							}
								
							preg_match($pattern, $doprogress[$id], $matches1);//匹配获取数字部分
							if($matches1[0] != $doprogress[$id]){
								$msg[] = '填写进度格式不正确，必须为全数字';
							}
						}
					}
				}
			}
			
			if (empty($msg)) {
				$db = App_Db::localDb();
				$db->beginTransaction();
				try {
					$row = array('content'=>$content, 'qa_ids'=>json_encode($qa_ids), 'add_time'=>date('Y-m-d H:i:s', time()), 'add_user'=>$_SESSION['User']['user']['user_id']);
					$aid = $daily->add($row);//获取新增daily的id值
					if(!empty($qa_ids)){
						foreach ($qa_ids as $id){
							$qatimepro->add(array('qa_item'=>$id,'daily'=>$aid,'time'=>$dotime[$id],'progress'=>$doprogress[$id],'add_time'=>date('Y-m-d H:i:s', time())));
						}
					}
					$db->commit();
					$msg[] = '日报发布成功';
				} catch (Exception $e) {
					$db->rollback();
					$msg[] = '日报发布失败';
				}
				
				$this->view->msg = $msg;
			}else{
				$this->view->msg = $msg;
			}
		}
		$qaitemModel = App_Model::factory('QaItem');
		
		$relateList = array_merge(
				$qaitemModel->getQaItemByDate(date('Y-m-d 00:00:00',strtotime('-1 day')),date('Y-m-d 00:00:00',strtotime('+1 day')),$_SESSION['User']['user']['user_id'])->toArray(),
				$qaitemModel->getQaItem(array('task_id'=>$_SESSION['User']['user']['user_id'],'statu'=>1))->toArray()
		);//相关联的任务
		$this->view->relateList = $relateList;
	}
	
	/**修改日报*/
	public function updatedailyAction()
	{
		$dailyModel = App_Model::factory('Daily');
		$id = $this->getRequest()->getParam('id', '');
		
		if($this->getRequest()->isPost()) {
		    $content = htmlspecialchars_decode($this->getRequest()->getParam('editor', ''));
		    //$qa_ids = $this->getRequest()->getParam('qa_ids', '');
		    
		    $row = array('content'=>$content);
		    
		    if ($dailyModel->update($row,intval($id))) {
		    	$msg[] = '修改成功';
		    } else {
		    	$msg[] = '修改失败';
		    }
		    $this->view->msg = $msg;
		}
		
		if (!empty($id)){
			$daily = $dailyModel->getOne($id)->toArray();
			$this->view->daily = $daily;
		    $this->view->id = $id;
		}
		
		/**$qaitemModel = App_Model::factory('QaItem');
		$qatimeproModel = App_Model::factory('QaTimepro');
		$relateList = array_merge(
				$qaitemModel->getQaItemByDate(date('Y-m-d 00:00:00'),date('Y-m-d 00:00:00',strtotime('+1 day')),$_SESSION['User']['user']['user_id'])->toArray(),
				$qaitemModel->getQaItem(array('task_id'=>$_SESSION['User']['user']['user_id'],'statu'=>1))->toArray()
		);//相关联的任务
		$qatimepro = $qatimeproModel->getQaTimepro(array('daily'=>$id))->toArray();
		foreach($qatimepro as $v){
			$qatimeprolist[$v['qa_item']] = $v;
		}
		unset($qatimepro);
		$this->view->relateList = $relateList;
		$this->view->qatimeprolist = $qatimeprolist;*/
	}
	
	/**日报总览*/
	public function dailysAction()
	{
		$user = App_User::getCurrentUser();
		if($user->is_admin){
			$daily = App_Model::factory('Daily');
			$userModel = App_Model::factory('User');
			$qatimeproModel = App_Model::factory('QaTimepro');
			$qaitemModel = App_Model::factory('QaItem');
			
			$page = $this->getRequest()->getParam('page',1);
			$title = $this->getRequest()->getParam('title');
			$starttime = $this->getRequest()->getParam('starttime');
			$endtime = $this->getRequest()->getParam('endtime');
			$adduser = $this->getRequest()->getParam('add_user','');
			
			$limit = 10;//每页显示数
			$list  = $daily->listAll($title,$adduser,$starttime,$endtime,$page,$limit)->toArray();
			foreach ($list as &$v){
				$qa = array();
				foreach (json_decode($v['qa_ids']) as $qv){
					$qatimepro = $qatimeproModel->getQaTimepro(array('qa_item'=>$qv,'daily'=>$v['id']))->toArray();
					$qaitem = $qaitemModel->getOne($qv)->toArray();
					$qa[] = array('title'=>$qaitem['title'],'time'=>$qatimepro[0]['time'],'progress'=>$qatimepro[0]['progress']);
				}
				$v['qa'] = $qa;
				$v['add_user'] = $userModel->getOne($v['add_user'])->true_name;
			}
			
			/**生成分页对象*/
			$num = $daily->getTotalNum($title,$adduser,$starttime,$endtime);
			$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_Null($num));
			$paginator->setItemCountPerPage($limit); // 每页10条
			$paginator->setCurrentPageNumber($page);
			$this->view->paginator = $paginator ; //将paginator传到view层
			$this->view->adduser = $adduser;
			$this->view->list = $list;
			$this->view->user_name = trim($userModel->getOne($adduser)->true_name);
		}else{
			exit('You have no priviage');
		}
	}
}
